<template>
  <a-radio-group v-if="tagType=='radio'" @change="handleInput" :value="getValueSting" :disabled="disabled">
    <a-radio v-for="(item, key) in dictOptions" :key="key" :value="item.itemValue">{{ item.itemText || item.label }}</a-radio>
  </a-radio-group>

  <a-radio-group v-else-if="tagType=='radioButton'"  buttonStyle="solid" @change="handleInput" :value="getValueSting" :disabled="disabled">
    <a-radio-button v-for="(item, key) in dictOptions" :key="key" :value="item.itemValue">{{ item.itemText || item.label}}</a-radio-button>
  </a-radio-group>

  <a-select v-else-if="tagType=='select'" :getPopupContainer = "getPopupContainer" :placeholder="placeholder" :disabled="disabled" :value="getValueSting" @change="handleInput">
    <a-select-option :value="undefined" v-if="empty">请选择</a-select-option>
    <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.itemValue">
      <span style="display: inline-block;width: 100%" :title=" item.itemText || item.label ">
        {{ item.itemText || item.label }}
      </span>
    </a-select-option>
  </a-select>
</template>

<script>
import { getDictItemsFromCache } from '@/services/dict'
import { initDictOptions } from './ZDictSelectUtil'
export default {
  name: 'ZDictSelectTag',
  props: {
    dictCode: String,
    placeholder: {
      type: String,
      default: '请选择'
    },
    triggerChange: Boolean,
    disabled: Boolean,
    value: [String, Number],
    type: String,
    getPopupContainer: {
      type: Function,
      default: () => document.body
      // default: (node) => node.parentNode
    },
    options: Array,
    defaultValue: Boolean,
    empty: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      dictOptions: [],
      tagType: ''
    }
  },
  watch: {
    dictCode: {
      immediate: true,
      handler() {
        this.initDictData()
      }
    }
  },
  created() {
    if (!this.type || this.type === 'list') {
      this.tagType = 'select'
    } else {
      this.tagType = this.type
    }
  },
  computed: {
    getValueSting() {
      // update-begin author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
      // 当有null或“” placeholder不显示
      return this.value != null ? this.value.toString() : undefined
      // update-end author:wangshuai date:20200601 for: 不显示placeholder的文字 ------
    }
  },
  methods: {
    initDictData() {
      if (this.options) {
        this.dictOptions = []
        this.options.forEach(item => {
          this.dictOptions.push({
            itemText: item.label,
            itemValue: item.value
          })
        })
        return
      }
      // 优先从缓存中读取字典配置
      if (getDictItemsFromCache(this.dictCode)) {
        const dictData = getDictItemsFromCache(this.dictCode)
        this.dictOptions = dictData.items
        if (!this.value && this.defaultValue) this.handleInput(dictData.dictDefaultValue)
        return
      }
      // 根据字典Code, 初始化字典数组
      initDictOptions(this.dictCode).then(res => {
        if (res.success && res.result) {
          this.dictOptions = []
          res.result.items.forEach(item => {
            this.dictOptions.push({
              itemText: item.itemText,
              itemValue: item.itemValue
            })
          })
          if (!this.value && this.defaultValue) this.handleInput(res.result.dictDefaultValue)
        }
      })
    },
    handleInput(e = '') {
      let val
      if (Object.keys(e).includes('target')) {
        val = e.target.value
      } else {
        val = e
      }
      if (this.triggerChange) {
        this.$emit('change', val)
      } else {
        this.$emit('input', val)
      }
    },
    setCurrentDictOptions(dictOptions) {
      this.dictOptions = dictOptions
    },
    getCurrentDictOptions() {
      return this.dictOptions
    }
  }
}
</script>

<style scoped>
</style>
